<?php
/**
 * @package PWE.controler
 * @author Gmax <gmaxfr78@gmail.com>
 * gestion des appels de base aux modeles protocol CRUD (create, read, update, delete); extends PWE
 */
class controler extends PWE{
    function __construct() {
        parent::__construct();
    }
    
    public function execute($query){
        $action = $query['action'];
        $modele = $query['modele'];
        $modele_id = (isset($query['modele_id']))?$query['modele_id']:0;
        $datas = $query['data'];
        $module = (isset($query['module']))?$query['module']:'';
        switch($action){
            case 'create':{
                return $this->create($modele,$datas,$module);
            }
            case 'read':{
                return $this->read($modele,$modele_id,$module);
            }
            case 'update':{
                return $this->update($modele,$modele_id,$datas,$module);
            }
            case 'delete':{
                return $this->delete($modele,$modele_id,$module);
            }
        }
    }
    public function getOutput(){
        navigation::init();
        if(PWE::isInstallMode()){
            if(navigation::isAjaxRequest()){
                echo ajax::getAjax();
                ob_end_flush();
                exit;
            }else{
                header('location: '.ADMIN_PATH.'install.php');
                exit;
            }
        }else{
            if(navigation::isAjaxRequest()){
                echo ajax::getAjax();
                ob_end_flush();
                exit;
            }elseif(navigation::isSitemap()){
                echo sitemap::getSiteMap();
                ob_end_flush();
                exit;
            }elseif(navigation::isRedirection()){
                navigation::getRedirection()->doRedirection();
                ob_end_flush();
                exit;
            }elseif(navigation::is404()){
                header("HTTP/1.0 404 Not Found");
            }
        }
    }
    
    /**
     * 
     * @param string $modele
     * @param array $datas
     * @param string $module
     * @return array
     */
    public function create($modele,$datas,$module=''){
        $result = array('success'=>false,'datas'=>array());
        $instance = $this->getModele($modele,0,$module);
        if($instance!==null){
            if(count($datas)>0 && $instance->setDatas($datas)){
                $result = $instance->save();
                if($result['success']){
                    navigation::setAdminAction('update');
                    navigation::setAdminModeleId($result['datas']['id']);
                    $this->pingSe();
                    return $result;
                }
            }
        }
        return $result;
    }
    
    /**
     * 
     * @param string $modele
     * @param int $id
     * @param string $module
     * @return array
     */
    public function read($modele,$modele_id=0,$module=''){
        $result = array();
        $instance = $this->getModele($modele,$modele_id,$module);
        if($instance!==null){
            $result = $instance->getDatas();
        }
        return $result;
    }
    
    /**
     * 
     * @param string $modele
     * @param int $modele_id
     * @param array $datas
     * @param string $module
     * @return array
     */
    public function update($modele,$modele_id=0,$datas=array(),$module=''){
        $result = array('success'=>false,'datas'=>array());
        $instance = $this->getModele($modele,$modele_id,$module);
        if($instance!==null){
            if(count($datas)>0 && $instance->setDatas($datas)){
                $result = $instance->update();
            }
        }
        return $result;
    }
    
    /**
     * 
     * @param string $modele
     * @param int $modele_id
     * @param string $module
     * @return array
     */
    public function delete($modele,$modele_id=0,$module=''){
        $result = array('success'=>false);
        $instance = $this->getModele($modele,$modele_id,$module);
        if($instance!==null){
            $result = $instance->delete();
        }
        return $result;
    }
    
    /**
     * 
     * @param string $modele
     * @param int $modele_id
     * @param string $module
     * @return \modele|mixed
     */
    public function getModele($modele,$modele_id=0,$module=''){
        $instance = null;
        if($modele==='logo'){
            $modele = 'media';
        }
        if($modele!==''){
            if($modele_id>0){
                if($module!=='' && class_exists('modules_'.$module.'_'.$modele)){
                    eval('$instance = new modules_'.$module.'_'.$modele.'('.$modele_id.');');
                }elseif(class_exists('modele_'.$modele)){
                    eval('$instance = new modele_'.$modele.'('.$modele_id.');');
                }
            }else{
                if($module!=='' && class_exists('modules_'.$module.'_'.$modele)){
                    eval('$instance = new modules_'.$module.'_'.$modele.'();');
                }elseif(class_exists('modele_'.$modele)){
                    eval('$instance = new modele_'.$modele.'();');
                }
            }
        }
        return $instance;
    }
    
    /**
    * ping les serach engine pour soumettre le sitemap à jour
    */
    public function pingSe(){
        if(!DEBUG_MODE){
            foreach(self::$pingSeUrl as $ping){
                $ping .= $this->getBaseUrl().'sitemap.xml';
                $curl_handle=curl_init();
                curl_setopt($curl_handle,CURLOPT_URL,$ping);
                curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
                curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
                $buffer = curl_exec($curl_handle);
                curl_close($curl_handle);
            }
        }
    }
}
?>